package com.filldream.fastboot.web.controller.pc;

import com.filldream.fastboot.base.entity.BaseParam;
import com.filldream.fastboot.base.request.IdListSTO;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.pc.baseParam.BaseParamParentPageSTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.PassTokenAnnotation;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.core.manager.BaseParamManager;
import com.filldream.fastboot.core.service.BaseParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

@Api(tags = "PC-业务基础参数管理")
@RestController
@RequestMapping("/pc/bizBaseParam")
public class PcBizBaseParamController {

    @Autowired
    private BaseParamService baseParamService;

    @Log(value = "参数列表")
    @ApiOperation(value = "参数列表")
    @GetMapping(value = "/page")
    public P<BaseParam> page( @Valid BaseParamParentPageSTO baseParamParentPageSTO) {
        baseParamParentPageSTO.setParamType(2);
        return baseParamService.page(baseParamParentPageSTO);
    }

    @PassTokenAnnotation
    @ApiOperation(value = "参数列表-树")
    @GetMapping(value = "/tree")
    @RolePermissions("")
    public R tree() {
        return baseParamService.tree(2);
    }

    @Log(value = "新增参数")
    @ApiOperation(value = "新增参数")
    @PostMapping(value = "/add")
    @RolePermissions("bizBaseParam:add")
    public B add(@RequestBody BaseParam baseParam) {
        baseParam.setParamType(2);
        return B.checkSuccess(baseParamService.add(baseParam.setId(null)));
    }

    @Log(value = "参数详情")
    @ApiOperation(value = "参数详情",notes = "传入参数ID")
    @PostMapping(value = "/get")
    @RolePermissions("bizBaseParam:get")
    public R<BaseParam> get(@RequestBody IdSTO idSTO) {
        BaseParam baseParam = baseParamService.getById(idSTO.getId());
        if( baseParam != null && baseParam.getParamType().equals(1) ){
            return R.fail("参数不存在");
        }
        return R.autoSuccess( baseParam );
    }


    @Log(value = "删除参数")
    @ApiOperation(value = "删除参数",notes = "")
    @PostMapping(value = "/del")
    @RolePermissions("bizBaseParam:del")
    public B del(@RequestBody IdListSTO idListSTO) {
        return R.autoSuccess(BaseParamManager.del(idListSTO.getIds()));
    }

    @Log(value = "编辑参数")
    @ApiOperation(value = "编辑参数")
    @PostMapping(value = "/update")
    @RolePermissions("bizBaseParam:update")
    public B update(@RequestBody BaseParam baseParam) {
        baseParam.setParamType(2);
        return B.checkSuccess(baseParamService.modify(baseParam));
    }

}
